// 配置路由的地方
import Vue from 'vue'
import VueRouter from 'vue-router'

//使用插件
Vue.use(VueRouter)

//引入路由组件
import Home from '@/views/Home'
import Search from '@/views/Search'
import Login from '@/views/Login'
import Register from '@/views/Register'
import Detail from '@/views/Detail'


//先把VueRouter原型对象上的push  保存一份
let originPush = VueRouter.prototype.push;
let originReplace = VueRouter.prototype.replace;


//重新push|replace
//第一个参数   告诉原来的push方法  你往哪里跳（传递哪些参数）

VueRouter.prototype.push = function(location,resolve,reject){
    if(resolve && reject){
        originPush.call(this,location,resolve,reject);
    }else{
        originPush.call(this,location,()=>{},()=>{});
    }
}

VueRouter.prototype.replace = function(location,resolve,reject){
    if(resolve && reject){
        originReplace.call(this,location,resolve,reject);
    }else{
        originReplace.call(this,location,()=>{},()=>{});
    }
}

//配置路由：
export default new VueRouter({
    //配置路由
    routes:[
        {
            path:"/home",
            component:Home,
            meta:{show:true}
        },
        {
            path:'/search/:keyword?',
            component:Search,
            meta:{show:true},
            name:"search",
            //路由组件能不能传递props数据？
            //布尔值的写法:只能传递params
            // props:true
            //对象写法
            //props:{a:1,b:2}
            //函数写法
            props:($route) =>{
                return {keyword:$route.params.keyword,k:$route.query.k}
            }
        },
        {
            path:'/login',
            component:Login,
            meta:{show:false}
        },
        {
            path:'/register',
            component:Register,
            meta:{show:false}
        },
        //重定向  在项目跑起来的时候 访问/  立马让他定向到首页
        {
            path:'*',
            redirect:'/home'
        },
        {
            path:'/detail',
            components:'Detail'
        }
    ]
})